/*
 * @Author: dadadaXU 1413107032@qq.com
 * @Date: 2025-01-25 12:06:03
 * @LastEditors: dadadaXU 1413107032@qq.com
 * @LastEditTime: 2025-01-25 12:15:21
 * @FilePath: \LeetCode\206.反转链表.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */

struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
    ListNode *reverseList(ListNode *head)
    {
        if (head == nullptr)
            return nullptr;

        ListNode *p = head->next;
        head->next = nullptr;
        while (p != nullptr)
        {
            /* 头插法 */
            ListNode *temp = p->next;
            p->next = head;
            head = p;

            p = temp;
        }

        return head;
    }
};
// @lc code=end
